// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })

    const userInfo = wx.getStorageSync('user2');
    if (userInfo && userInfo.class === 'stu') {
      this.setStudentTabBar();
    } else {
      this.setDefaultTabBar();
    }
  },

  globalData: {
    userInfo: null
  },

  updateTabBar() {
    const userInfo = wx.getStorageSync('user2');
    if (userInfo && userInfo.class === 'stu') {
      this.setStudentTabBar();
    } else {
      this.setDefaultTabBar();
    }
  },

  setStudentTabBar() {
    wx.setTabBarItem({
      index: 0,
      text: '学生活动',
      iconPath: '/image/student-activity.png',
      selectedIconPath: '/image/student-activity-active.png'
    });
    wx.setTabBarItem({
      index: 1,
      text: '扫一扫',
      iconPath: '/image/scan.png',
      selectedIconPath: '/image/scan.png'
    });
    wx.setTabBarItem({
      index: 2,
      text: '我的',
      iconPath: '/image/me.png',
      selectedIconPath: '/image/me-active.png'
    });
  },

  setDefaultTabBar() {
    wx.setTabBarItem({
      index: 0,
      text: '活动',
      iconPath: '/image/index.png',
      selectedIconPath: '/image/index-active.png'
    });
    wx.setTabBarItem({
      index: 1,
      text: '扫一扫',
      iconPath: '/image/scan.png',
      selectedIconPath: '/image/scan.png'
    });
    wx.setTabBarItem({
      index: 2,
      text: '我的',
      iconPath: '/image/me.png',
      selectedIconPath: '/image/me-active.png'
    });
}
})
